package com.banyou.banyou.customviews;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.banyou.banyou.R;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by LX on 2017/1/28.
 */
/* ----------BigGod be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * 项目名称:WheelMedical
 * 包名:com.yyxk.wheelmedical.customview
 * 类描述:
 * 创建人:LX
 * 创建时间:2017/1/28
 * 修改人:LX
 * 修改时间:
 * 修改备注
 */
public class LoadingRelativeLayout extends RelativeLayout {

    public static final int NETWORK_ERROR = 1;//网络异常
    public static final int NETWORK_LOADING = 2;//加载中
    public static final int NO_DATA = 3;//没有数据
    public static final int HIDE_LAYOUT = 4;//加载完成
    private int mErrorState;

    private Animation mAnimation;
    private Context mContext;
    private View mRootView;
    private ViewHolder mHolder;

    public LoadingRelativeLayout(Context context) {
        super(context);
        init(context);
    }

    public LoadingRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    /**
     * 初始化
     *
     * @param context
     */
    private void init(Context context) {
        mContext = context;
        //加载loading动画，并附着在根视图上
        mRootView = LayoutInflater.from(mContext).inflate(R.layout.loading_logo, null);
        mHolder = new ViewHolder(mRootView);
//        mHolder.mErrorImg.setVisibility(INVISIBLE);
        mHolder.mReloadBtn.setVisibility(INVISIBLE);
        initListener();
        mAnimation = AnimationUtils.loadAnimation(mContext, R.anim.logo_rotation);
        mHolder.mReloadBtn.setVisibility(INVISIBLE);
        mHolder.mLoadingLogo.setVisibility(INVISIBLE);
        addView(mRootView, getChildCount(), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        View child = getChildAt(0);
        child.setVisibility(GONE);


    }

    public void dismiss() {
        mErrorState = HIDE_LAYOUT;
        setVisibility(View.GONE);
    }

    public int getErrorState() {
        return mErrorState;
    }

    public boolean isLoadError() {
        return mErrorState == NETWORK_ERROR;
    }

    public boolean isLoading() {
        return mErrorState == NETWORK_LOADING;
    }

    public void setErrorType(int i) {
        setVisibility(View.VISIBLE);
        switch (i) {
            case NETWORK_ERROR:
                getChildAt(0).setVisibility(VISIBLE);
                mErrorState = NETWORK_ERROR;
                mHolder.mLoadingLogo.setVisibility(GONE);
                mHolder.mErrorImg.setVisibility(VISIBLE);
                mHolder.mReloadBtn.setVisibility(VISIBLE);
                mHolder.mErrorImg.setText("网络貌似出现了问题。。。");
                //弹出网络异常的提示
                break;
            case NETWORK_LOADING:
                mErrorState = NETWORK_LOADING;
                getChildAt(0).setVisibility(VISIBLE);
                mHolder.mLoadingLogo.setAnimation(mAnimation);
                mHolder.mLoadingLogo.setVisibility(VISIBLE);
                mHolder.mErrorImg.setVisibility(GONE);
                mHolder.mReloadBtn.setVisibility(GONE);
                break;
            case NO_DATA:
                mErrorState = NO_DATA;
                getChildAt(0).setVisibility(VISIBLE);
                mHolder.mLoadingLogo.setVisibility(GONE);
                mHolder.mErrorImg.setVisibility(VISIBLE);
                mHolder.mReloadBtn.setVisibility(VISIBLE);
                mHolder.mErrorImg.setText("这里目前没有数据");
                break;
            case HIDE_LAYOUT:
                getChildAt(0).setVisibility(GONE);
                mErrorState = HIDE_LAYOUT;
                setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    /**
     * 初始化监听器
     */
    private void initListener() {
        mHolder.mReloadBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnReloadListener.onReload();
            }
        });
    }


    /**
     * 重新加载接口
     */
    public interface OnReloadListener {
        void onReload();
    }

    private OnReloadListener mOnReloadListener;//重新加载监听

    /**
     * 外部传入重新加载监听
     *
     * @param listener
     */
    public void setOnReloadListener(OnReloadListener listener) {
        mOnReloadListener = listener;
    }

    /**
     * 开始加载
     */
    public void startLoading() {
        showOtherItem();
        View child = getChildAt(0);
        child.setVisibility(GONE);
        setClickable(false);
        setFocusable(false);
        mHolder.mReloadBtn.setVisibility(INVISIBLE);
    }

    /**
     * 结束加载
     */
    public void stopLoading() {
        View child = getChildAt(0);
        child.setVisibility(GONE);
    }

    /**
     * 加载时出现异常
     */
    public void errorLoading() {
        hideOtherItem();
        View child = getChildAt(0);
        child.setVisibility(VISIBLE);
        mHolder = new ViewHolder(child);
        mHolder.mErrorImg.setText("网络貌似出现问题了...");
        mHolder.mReloadBtn.setVisibility(INVISIBLE);
        setClickable(true);
        setFocusable(true);
    }

    /**
     * 加载时出现异常，显示Btn
     *
     * @param showBtn
     */
    public void errorLoading(boolean showBtn) {
        errorLoading();
        mHolder.mReloadBtn.setVisibility(VISIBLE);
    }

    /**
     * 加载时出现空现象
     */
    public void emptyLoading() {
        hideOtherItem();
        View child = getChildAt(0);
        child.setVisibility(VISIBLE);
        mHolder = new ViewHolder(child);
        mHolder.mReloadBtn.setVisibility(INVISIBLE);
        mHolder.mErrorImg.setText("这里啥都没有...");
    }

    /**
     * 加载时出现空现象，传入文字
     *
     * @param text
     */
    public void emptyLoading(String text) {
        emptyLoading();
        mHolder.mErrorImg.setText(text);
    }

    /**
     * 加载时出现空现象，传入文字，显示BTN
     *
     * @param text
     * @param showBtn
     */
    public void emptyLoading(String text, boolean showBtn) {
        emptyLoading(text);
        if (showBtn) {
            mHolder.mReloadBtn.setVisibility(VISIBLE);
        } else {
            mHolder.mReloadBtn.setVisibility(INVISIBLE);
        }
    }

    /**
     * 当view加载完毕，将loadingview加载入ViewGroup中
     */
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.i("tagggg", "getChildCount=" + getChildCount());
//        removeView(mRootView);

//        init(mContext);
    }

    /**
     * 隐藏其他
     */
    private void hideOtherItem() {
        for (int i = 1; i < getChildCount(); i++) {
            getChildAt(i).setVisibility(INVISIBLE);
        }
    }

    /**
     * 显示其他
     */
    private void showOtherItem() {
        for (int i = 1; i < getChildCount(); i++) {
            getChildAt(i).setVisibility(VISIBLE);
        }
    }


    static class ViewHolder {
        @BindView(R.id.loading_logo)
        ImageView mLoadingLogo;
        @BindView(R.id.reload_btn)
        Button mReloadBtn;
        @BindView(R.id.error_img)
        TextView mErrorImg;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
